﻿using KWT.DRG_DIP.API.Infrastructure;
using KWT.DRG_DIP.DO.Group.DRGGroup;
using KWT.DRG_DIP.DO.System;
using KWT.DRG_DIP.DTO.Group;
using KWT.DRG_DIP.ISvc;
using KWT.DRG_DIP.PO.Basic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace KWT.DRG_DIP.API.Controllers.Grouper
{
    /// <summary>
    /// DRG分组策略字典
    /// </summary>
    [AllowAnonymous]
    [ApiExplorerSettings(GroupName = "grouper")]
    public class DRGController : GrouperControllerBase
    {
        public IPolicyEnumSvc policyEnumSvc { get; set; }

        public IDrgGrouperSvc grouper { get; set; }

        /// <summary>
        /// RuleMDC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_MDC>>), 200)]
        public OutputResult RuleMDC()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleMDC(tenantID);
            return Output(true, "", "", list);
        }

        /// <summary>
        /// RuleADRG
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_ADRG>>), 200)]
        public OutputResult RuleADRG()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleADRG(tenantID);
            return Output(true, "", "", list);
        }

        /// <summary>
        /// RuleDRG
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_DRG>>), 200)]
        public OutputResult RuleDRG()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleDRG(tenantID);
            return Output(true, "", "", list);
        }

        /// <summary>
        /// RuleMCC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_MCC>>), 200)]
        public OutputResult RuleMCC()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleMCC(tenantID);
            return Output(true, "", "", list);
        }

        /// <summary>
        /// RuleCC
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_CC>>), 200)]
        public OutputResult RuleCC()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleCC(tenantID);
            return Output(true, "", "", list);
        }

        /// <summary>
        /// RuleExclude
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_Exclude>>), 200)]
        public OutputResult RuleExclude()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleExclude(tenantID);
            return Output(true, "", "", list);
        }

        /// <summary>
        /// RuleNotDiagnose
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_NotDiagnose>>), 200)]
        public OutputResult RuleNotDiagnose()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleNotDiagnose(tenantID);
            return Output(true, "", "", list);
        }

        /// <summary>
        /// RuleNotOperation
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<Rule_NotOperation>>), 200)]
        public OutputResult RuleNotOperation()
        {
            string tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            var list = policyEnumSvc.GetRuleNotOperation(tenantID);
            return Output(true, "", "", list);
        }

        #region DRG分组器,返回病种对应的标杆信息
        /// <summary>
        /// DRG分组器,返回病种对应的标杆信息
        /// </summary>
        /// <param name="input">分组器入参</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public OutputResult DRGGroup([FromBody] InGroupPara input)
        {
            string? tenantID = HttpContext.Request.Headers["TenantID"];
            if (string.IsNullOrEmpty(tenantID))
            {
                return Fail("缺少租户ID");
            }
            //获得DIP分组结果
            var result = grouper.GroupExecue(input, tenantID);

            if (!result.Success)
            {
                return Output(false, "", result.msg, result);
            }

            //根据drg结果获得标杆信息
            var para = ef.Set<Sys_DRG_Para>().SingleOrDefault(x => x.DRG_Code == result.strResult);
            return Output(true, "", "", para);

        }
        #endregion
    }
}
